package com.file.system.controller.system;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.file.system.entity.res.CommonResult;
import com.file.system.entity.res.DataPage;
import com.file.system.entity.system.WebPost;
import com.file.system.service.system.IWebPostService;
import com.file.system.tools.regular.RegularUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;


/**
* 岗位
*
* @author jch
*/
@RestController
@Validated
@RequestMapping("/web/post")
public class WebPostController {

    @Autowired
    private IWebPostService service;

    /**
    * 查询岗位分页
    *
    * @param pageIndex 页数
    * @param pageSize  每页条数
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/page/{pageIndex}/{pageSize}")
    public CommonResult<DataPage<List<WebPost>>> page(@PathVariable
                                                          @Min(value = 1, message = "页数最小1")
                                                          Integer pageIndex,
                                                          @PathVariable
                                                          @Min(value = 1, message = "每页条数最小1")
                                                          @Max(value = 100, message = "每页条数最大100")
                                                          Integer pageSize,
                                                          @RequestBody @Validated WebPost webPost
        ) {
        QueryWrapper<WebPost> query = new QueryWrapper<>();
        //根据sort字段升序排序
        query.orderByAsc("a.sort");
        //查询状态为0的数据
        query.eq("a.is_del", 0);
        //查询名称包含webPost.getName()的数据
        query.like(StrUtil.isNotEmpty(webPost.getName()),"a.name", webPost.getName());
        //查询code包含webPost.getCode()的数据
        query.like(StrUtil.isNotEmpty(webPost.getCode()),"a.code", webPost.getCode());
        //查询dict_on_off包含webPost.getDictOnOff()的数据
        query.like(StrUtil.isNotEmpty(webPost.getDictOnOff()),"a.dict_on_off", webPost.getDictOnOff());
        //查询description包含webPost.getDescription()的数据
        query.like(StrUtil.isNotEmpty(webPost.getDescription()),"a.description", webPost.getDescription());
        //查询集合
        DataPage<List<WebPost>> dp = service.page(pageIndex, pageSize, query);
        return CommonResult.ok().setResult(dp);
    }

    /**
    * 通过ids查询岗位集合
    *
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/listByIds")
    public CommonResult<List<WebPost>> listByIds(@RequestBody List<Long> ids) {
        //查询集合
        List<WebPost> list = service.listByIds(ids);
        return CommonResult.ok().setResult(list);
    }

    /**
    * 查询所有非删除状态岗位
    *
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/listAll")
    public CommonResult<List<WebPost>> listAll() {
        //查询集合
        List<WebPost> list = service.listAll();
        return  CommonResult.ok().setResult(list);
    }


    /**
    * 通过id查询岗位
    *
    * @param id 岗位id
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/id/{id:" + RegularUtil.ID + "}")
    public CommonResult<WebPost> getById(@PathVariable
                                              @Min(value = 1, message = "id长度最小1")
                                              Long id) {
        //查询集合
        WebPost webPost = service.getById(id);
        return CommonResult.ok().setResult(webPost);
    }

    /**
    * 通过id删除岗位
    *
    * @param id id
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/remove/{id:" + RegularUtil.ID + "}")
    public CommonResult removeById(@PathVariable
                            @Min(value = 1, message = "id长度最小1")
                            Long id) {
        //返回
        return CommonResult.check(service.removeById(id));
    }

    /**
    * 通过ids删除岗位
    *
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/removeByIds")
    public CommonResult removeById(@RequestBody List<Long> ids) {
        //返回
        return CommonResult.check(service.removeByIds(ids));
    }


    /**
    * 通过id编辑岗位
    *
    * @param webPost 岗位
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/updateById")
    public CommonResult<WebPost> updateById(@RequestBody @Validated(WebPost.update.class) WebPost webPost) {
        //编辑修改
        return CommonResult.check(service.updateById(webPost)).setResult(webPost);
    }


    /**
    * 添加岗位
    *
    * @param webPost 岗位
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/save")
    public CommonResult<WebPost> save(@RequestBody @Validated(WebPost.add.class) WebPost webPost) {
        //添加岗位
        return CommonResult.check(service.save(webPost)).setResult(webPost);
    }
}

